package boot3.service.impl;

import boot3.mapper.ReservationMapper;
import boot3.mapper.UserMapper;
import boot3.pojo.Reservation;
import boot3.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

@Service
public class ReservationServiceImpl implements ReservationService {
    @Autowired
    private ReservationMapper reservationMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Reservation> selectAllReservationByStudentID(String student_id) {
        return reservationMapper.selectAllReservationByStudentID(student_id);
    }

    @Override
    public List<Reservation> selectAllReservationByTeacherID(String teacher_id) {
        return reservationMapper.selectAllReservationByTeacherID(teacher_id);
    }

    @Override
    public Boolean createReservation(Reservation reservation) {
        //设置reservaiton的id
        int length=reservationMapper.selectAllReservation().size();
        int index=reservationMapper.selectAllReservation().isEmpty()?0:Integer.parseInt(reservationMapper.selectAllReservation().get(length-1).getReserve_number())+1;
        reservation.setReserve_number(String.valueOf(index));
        //设置创建时间
        reservation.setInitiation_time(getTime());
        //设置创建状态
        reservation.setState("待审批");
        //设置学生的姓名
        reservation.setStudent_name(userMapper.getUserName(reservation.getStudent_id()));
        return reservationMapper.createReservation(reservation);
    }

    @Override
    public Boolean deleteReservation(String reserve_number) {
        return reservationMapper.deleteReservation(reserve_number);
    }

    @Override
    public Boolean updateState(String reserve_number) {
        return reservationMapper.updateState(reserve_number);
    }

    @Override
    public Boolean updateStatePurpose(String reserve_number, String purpose) {
        return reservationMapper.updateStatePurpose(reserve_number, purpose);
    }

    //获取当前的时间
    private String getTime(){
        Calendar calendar= Calendar.getInstance();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return dateFormat.format(calendar.getTime());
    }
}
